
Control Statements
Now that you have got some basic understanding of how to write a Java program, it is now time to do real programming. In this tutorial you will learn how to control your program and make it do some computations depending on certain conditions. Java contains some control statements that allow you to do so. So, let's get started by explaining the if else conditional statement.
if Selection Statement
An if statement is the same as it sounds. If I have this condition then do something. Let's explain this in more details. Check the following text below.
if(condition)
do something
This means that if the condition is true, do some computations. Let's see this in real Java code. Check the program below.
public class If
{
public static void main(String args[])
{
int x = 3;
int y = 1;
if(x > y)
{
System.out.println("x is greater than y");
}
}
}
Output:
x is greater than y
In this code, we are defining two variables x and y. Then, we are comparing to see if x is greater that y. If so, then the program will print that x is greater than y.
Comparison Operators
You have seen the Arithmetic Operators in previous tutorials, now we will introduce the comparison operators used in Java. Check the table below for the use and description of these operators.
| Operator | Description |
|---|---|
| > | Greater than |
| < | Less than |
| >= | Greater or equal to |
| <= | Less or equal to |
| == | Equal to |
| != | Not equal to |
Nested if Statements
An if statement can have an if statement inside it. This is used if we want to do multiple checks depending on certain conditions. Let's take for example a program that reads three numbers from the user and determines the largest one. Check the code below.
import java.util.Scanner;
public class NestedIf
{
public static void main (String[] args)
{
Scanner input = new Scanner(System.in);
int x = input.nextInt();
int y = input.nextInt();
int z = input.nextInt();
if(x > y)
{
if(x > z)
{
System.out.println(x+" is the largest number");
}
}
if(y > x)
{
if(y > z)
{
System.out.println(y+" is the largest number");
}
}
if(z > x)
{
if(z > y)
{
System.out.println(z+" is the largest number");
}
}
}
}
Output:
2 10 5 10 is the largest number
This program is first checking if x is greater than y. If so, it will checks if x is also greater than z. If that condition is also true, it will print that x is the largest number. Also, the same is done for y and z.
Logical Operators
These operators are sometimes used to avoid nesting and make the code simpler and more readable. For example, when we checked for the lergest number between three variables, we had to nest our if statements. Therefore, we had to do something like this.
if(condition)
if(condition)
do something
However, to make things simpler, we could do something like this.
if(condition and condition)
Take a look at the following Logical Operators table and then check the code for the largest three numbers written using logical operators.
do something
| Operator | Description |
|---|---|
| && | Logical AND |
| || | Logical OR |
import java.util.Scanner;
public class Logical
{
public static void main (String[] args)
{
Scanner input = new Scanner(System.in);
int x = input.nextInt();
int y = input.nextInt();
int z = input.nextInt();
if(x > y && x > z)
{
System.out.println(x+" is the largest number");
}
if(y > x && y > z)
{
System.out.println(y+" is the largest number");
}
if(z > x && z > y)
{
System.out.println(z+" is the largest number");
}
}
}
if..else Statement
Now that you know how to handle conditions using if, let's move on to explaining what could be done if the condition is not true. Here is where the else statement's role comes.
if(condition)
Check the following code to understand how the else statement works.
do something
else
do something else
public static void main (String[] args)
{
int x = 10;
int y = 99;
if(x > y)
{
System.out.println(x+" is greater");
}
else
{
System.out.println(y+" is greater");
}
}
}
Output:
99 is greater
The else..if Statement
If you have more than two conditions two handle, the if..else statement may not come in handy because it only handles two conditions. Therefore, you can use if..else if..else. Let's revisit the program which determines the largest of three numbers.
public class Logical
{
public static void main (String[] args)
{
Scanner input = new Scanner(System.in);
int x = input.nextInt();
int y = input.nextInt();
int z = input.nextInt();
if(x > y && x > z)
{
System.out.println(x+" is the largest number");
}
else if(y > x && y > z)
{
System.out.println(y+" is the largest number");
}
else
{
System.out.println(z+" is the largest number");
}
}
}
In this program, we are using else if because we have more than two conditions. We have to check for x, y, and z to determine which is the largest.
Congratulations
You now know how to make your program respond to conditions using if and else. Move on to the next tutorial, but first check the following important notes.
Important Notes
More on else
You should keep in mind that the else statement is directly related to the if before it. Therefore, do not confuse yourself by not knowing to which if condition the else statement refer to.